package com.project.common.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class R<T> {

    // 是否成功
    private Boolean success;

    // 状态码
    private Integer code = 0;

    // 返回信息
    private String message = "";

    // 结果数据
    private T data;

    /**
     * 成功方法
     *
     * @return 无返回值
     */
    public static R<Void> success() {
        R<Void> r =  new R<>();
        r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return r;
    }

    public static R<Void> success(String message) {
        R<Void> r =  new R<>();
        r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMessage(message);
        return r;
    }

    public static <T> R<T> success(T data) {
        R<T> r =  new R<>();
        r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        r.setData(data);
        return r;
    }

    /**
     * 失败方法
     *
     * @return 无返回值
     */
    public static R<Void> error() {
        R<Void> r =  new R<>();
        r.setSuccess(ResultCodeEnum.UNKNOWN_REASON.getSuccess());
        r.setCode(ResultCodeEnum.UNKNOWN_REASON.getCode());
        r.setMessage(ResultCodeEnum.UNKNOWN_REASON.getMessage());
        return r;
    }

    public static R<Void> error(String message) {
        R<Void> r =  new R<>();
        r.setSuccess(ResultCodeEnum.UNKNOWN_REASON.getSuccess());
        r.setCode(ResultCodeEnum.UNKNOWN_REASON.getCode());
        r.setMessage(message);
        return r;
    }

    public static <T> R<T> error(T data) {
        R<T> r =  new R<>();
        r.setSuccess(ResultCodeEnum.UNKNOWN_REASON.getSuccess());
        r.setCode(ResultCodeEnum.UNKNOWN_REASON.getCode());
        r.setMessage(ResultCodeEnum.UNKNOWN_REASON.getMessage());
        r.setData(data);
        return r;
    }

    public static R<Void> setResultCodeEnum(ResultCodeEnum codeEnum){
        R<Void> r =  new R<>();
        r.setSuccess(codeEnum.getSuccess());
        r.setCode(codeEnum.getCode());
        r.setMessage(codeEnum.getMessage());
        return r;
    }
}